// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.audio.parameters;

import zombie.audio.FMODGlobalParameter;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.core.math.PZMath;
import zombie.iso.IsoChunk;
import zombie.iso.IsoChunkMap;
import zombie.iso.IsoWorld;

public final class ParameterZoneWaterSide extends FMODGlobalParameter {
    private int m_playerX = -1;
    private int m_playerY = -1;
    private int m_distance = 40;

    public ParameterZoneWaterSide() {
        super("ZoneWaterSide");
    }

    @Override
    public float calculateCurrentValue() {
        IsoGameCharacter isoGameCharacter = this.getCharacter();
        if (isoGameCharacter == null) {
            return 40.0F;
        } else {
            int int0 = (int)isoGameCharacter.getX();
            int int1 = (int)isoGameCharacter.getY();
            if (int0 != this.m_playerX || int1 != this.m_playerY) {
                this.m_playerX = int0;
                this.m_playerY = int1;
                this.m_distance = this.calculate(isoGameCharacter);
                if (this.m_distance < 40) {
                    this.m_distance = PZMath.clamp(this.m_distance - 5, 0, 40);
                }
            }

            return (float)this.m_distance;
        }
    }

    private int calculate(IsoGameCharacter isoGameCharacter) {
        if (IsoWorld.instance != null && IsoWorld.instance.CurrentCell != null && IsoWorld.instance.CurrentCell.ChunkMap[0] != null) {
            IsoChunkMap isoChunkMap = IsoWorld.instance.CurrentCell.ChunkMap[0];
            float float0 = Float.MAX_VALUE;

            for (int int0 = 0; int0 < IsoChunkMap.ChunkGridWidth; int0++) {
                for (int int1 = 0; int1 < IsoChunkMap.ChunkGridWidth; int1++) {
                    IsoChunk isoChunk = isoChunkMap.getChunk(int1, int0);
                    if (isoChunk != null && isoChunk.getNumberOfWaterTiles() == 100) {
                        float float1 = (float)(isoChunk.wx * 10) + 5.0F;
                        float float2 = (float)(isoChunk.wy * 10) + 5.0F;
                        float float3 = isoGameCharacter.x - float1;
                        float float4 = isoGameCharacter.y - float2;
                        if (float3 * float3 + float4 * float4 < float0) {
                            float0 = float3 * float3 + float4 * float4;
                        }
                    }
                }
            }

            return (int)PZMath.clamp(PZMath.sqrt(float0), 0.0F, 40.0F);
        } else {
            return 40;
        }
    }

    private IsoGameCharacter getCharacter() {
        IsoPlayer isoPlayer0 = null;

        for (int _int = 0; _int < IsoPlayer.numPlayers; _int++) {
            IsoPlayer isoPlayer1 = IsoPlayer.players[_int];
            if (isoPlayer1 != null
                && (isoPlayer0 == null || isoPlayer0.isDead() && isoPlayer1.isAlive() || isoPlayer0.Traits.Deaf.isSet() && !isoPlayer1.Traits.Deaf.isSet())) {
                isoPlayer0 = isoPlayer1;
            }
        }

        return isoPlayer0;
    }
}
